﻿using System;
using System.Linq;
using System.Security.Claims;
using Sustainalytics.Utils;
using Sustainalytics.Auth.Claims;

namespace Sustainalytics.OAuth.Services.Common
{

    public class PortfolioUserClaimsProcessor
    {
        public virtual PortfolioUserClaims GetPortfolioClaims()
        {
            var userClaims = new PortfolioUserClaims();

            if (ClaimsPrincipal.Current == null || ClaimsPrincipal.Current.Claims == null)
            {
                return userClaims;
            }

            string username = ClaimsPrincipal.Current.Identity.Name;
            var userId = ClaimsPrincipal.Current.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
            var userGivenName = ClaimsPrincipal.Current.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName).Value;
            var userSurname = ClaimsPrincipal.Current.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname).Value;
            var accountId = ClaimsPrincipal.Current.Claims.FirstOrDefault(c => c.Type == CustomClaimTypes.AccountId).Value;
            userClaims.UserFullName = userGivenName + " " + userSurname;
            userClaims.UserName = username;
            userClaims.UserId = new Guid(userId);
            userClaims.AccountId = new Guid(accountId);
            var accountUniverses = ClaimsPrincipal.Current.Claims.GetUniverseIds();

            userClaims.AddAccountUniverses(accountUniverses);
            return userClaims;
        }
    }
}